package cn.iocoder.yudao.module.trade.controller.admin.ordertransfer;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.trade.controller.admin.ordertransfer.vo.OrderTransferPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.ordertransfer.vo.OrderTransferRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.ordertransfer.vo.OrderTransferSaveReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordertransfer.OrderTransferDO;
import cn.iocoder.yudao.module.trade.service.ordertransfer.OrderTransferService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 订单转派记录")
@RestController
@RequestMapping("/trade/order-transfer")
@Validated
public class OrderTransferController {

    @Resource
    private OrderTransferService orderTransferService;

    @PostMapping("/create")
    @Operation(summary = "创建订单转派记录")
    @PreAuthorize("@ss.hasPermission('trade:order-transfer:create')")
    public CommonResult<Long> createOrderTransfer(@Valid @RequestBody OrderTransferSaveReqVO createReqVO) {
        return success(orderTransferService.createOrderTransfer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单转派记录")
    @PreAuthorize("@ss.hasPermission('trade:order-transfer:update')")
    public CommonResult<Boolean> updateOrderTransfer(@Valid @RequestBody OrderTransferSaveReqVO updateReqVO) {
        orderTransferService.updateOrderTransfer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单转派记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-transfer:delete')")
    public CommonResult<Boolean> deleteOrderTransfer(@RequestParam("id") Long id) {
        orderTransferService.deleteOrderTransfer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单转派记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-transfer:query')")
    public CommonResult<OrderTransferRespVO> getOrderTransfer(@RequestParam("id") Long id) {
        OrderTransferDO orderTransfer = orderTransferService.getOrderTransfer(id);
        return success(BeanUtils.toBean(orderTransfer, OrderTransferRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单转派记录分页")
    @PreAuthorize("@ss.hasPermission('trade:order-transfer:query')")
    public CommonResult<PageResult<OrderTransferRespVO>> getOrderTransferPage(@Valid OrderTransferPageReqVO pageReqVO) {
        PageResult<OrderTransferDO> pageResult = orderTransferService.getOrderTransferPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderTransferRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单转派记录 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-transfer:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderTransferExcel(@Valid OrderTransferPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderTransferDO> list = orderTransferService.getOrderTransferPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单转派记录.xls", "数据", OrderTransferRespVO.class,
                        BeanUtils.toBean(list, OrderTransferRespVO.class));
    }

}